//
//  PhoneNumAttributionViewController.m
//  LifeTools
//
//  Created by alex on 19/10/2016.
//  Copyright © 2016 chinaclick. All rights reserved.
//

#import "PhoneNumAttributionViewController.h"
#import "PhoneNumAttributionDataModel.h"

@interface PhoneNumAttributionViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *inputTextField;
@property (nonatomic, strong) UIButton *inquiryButton;
@property (nonatomic, strong) UIView *backView;


@end

@implementation PhoneNumAttributionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"手机号码归属地";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.titleLabel.text = @"请输入手机号码或者手机号码前7位:";
    self.inputTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.inquiryButton addTarget:self action:@selector(actionInquiry:) forControlEvents:UIControlEventTouchUpInside];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textAlignment = NSTextAlignmentLeft;

        [self.view addSubview:_titleLabel];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left).with.offset(ScreenWidth * 0.02);
            make.right.equalTo(self.view.mas_right).with.offset(- ScreenWidth * 0.02);
            make.top.equalTo(self.view.mas_top).with.offset(ScreenWidth * 0.02);
            make.height.mas_equalTo(ScreenWidth * 0.05);
        }];
    }
    return _titleLabel;
}

- (UITextField *)inputTextField {
    if (!_inputTextField) {
        _inputTextField = [[UITextField alloc] init];
        _inputTextField.layer.borderWidth = 0.3;
        _inputTextField.layer.cornerRadius = 5;
        _inputTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _inputTextField.delegate = self;
        [_inputTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//        NSString *holderText = @"点此输入";
//        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc]initWithString:holderText];
//        [placeholder addAttribute:NSForegroundColorAttributeName
//                           value:[UIColor redColor]
//                           range:NSMakeRange(0, holderText.length)];
//        [placeholder addAttribute:NSFontAttributeName
//                            value:[UIFont systemFontOfSize:13]
//                           range:NSMakeRange(0, holderText.length)];
//        _inputTextField.attributedPlaceholder = placeholder;
//        _inputTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [self.view addSubview:_inputTextField];
        
        [_inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left).with.offset(ScreenWidth * 0.02);
            make.right.equalTo(self.view.mas_right).with.offset(- ScreenWidth * 0.02);
            make.top.equalTo(_titleLabel.mas_bottom).with.offset(ScreenWidth * 0.05);
            make.height.mas_equalTo(31);
        }];
    }
    return _inputTextField;
}

- (UIButton *)inquiryButton {
    if (!_inquiryButton) {
        _inquiryButton = [[UIButton alloc] init];
        [_inquiryButton setTitle:@"查询" forState:UIControlStateNormal];
        [_inquiryButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_inquiryButton setBackgroundColor:RGBColor(21, 126, 251)];
        _inquiryButton.layer.cornerRadius = 5;
        [self.view addSubview:_inquiryButton];
        
        [_inquiryButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.view.mas_right).with.offset(- ScreenWidth * 0.02);
            make.top.equalTo(_inputTextField.mas_bottom).with.offset(ScreenWidth * 0.05);
            make.width.mas_equalTo(ScreenWidth * 0.3);
            make.height.mas_equalTo(ScreenWidth * 0.1);
        }];
    }
    return _inquiryButton;
}

- (void)actionInquiry:(UIButton *)sender {
    [WTRequestCenter getWithURL:@"http://apis.juhe.cn/mobile/get" parameters:@{@"phone":self.inputTextField.text, @"key":@"f5a35283c98981dc3e62c822e192c77f"} finished:^(NSURLResponse *response, NSData *data) {
        id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if (0 == [[obj objectForKey:@"error_code"] intValue]) {
            NSMutableArray *array = [NSMutableArray array];
            array = [obj objectForKey:@"result"];
            PhoneNumAttributionDataModel *model = [[PhoneNumAttributionDataModel alloc] init];
            model.zreacode = [[obj objectForKey:@"result"] objectForKey:@"areacode"];
            model.card = [[obj objectForKey:@"result"] objectForKey:@"card"];
            model.city = [[obj objectForKey:@"result"] objectForKey:@"city"];
            model.company = [[obj objectForKey:@"result"] objectForKey:@"company"];
            model.province = [[obj objectForKey:@"result"] objectForKey:@"province"];
            model.zip = [[obj objectForKey:@"result"] objectForKey:@"zip"];
            [self initResultView:model];
        } else {
            int errorcode = [[obj objectForKey:@"error_code"] intValue];
            switch (errorcode) {
                case 201101:
                    [STTextHudTool showText:@"手机号码不能为空"];
                    break;
                case 201102:
                    [STTextHudTool showText:@"错误的手机号码"];
                    break;
                case 201103:
                    [STTextHudTool showText:@"查询无结果"];
                    break;
                default:
                    break;
            }
        }
    } failed:^(NSURLResponse *response, NSError *error) {
        
    }];
}

- (void)initResultView:(PhoneNumAttributionDataModel *)model {
    _backView = [[UIView alloc] init];
    [_backView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.backView];
    
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view).with.offset(0);
        make.top.equalTo(_inquiryButton.mas_bottom).with.offset(ScreenWidth * 0.07);
    }];
    
    UILabel *provinceLabel = [[UILabel alloc] init];
    provinceLabel.font = [UIFont systemFontOfSize:13];
    provinceLabel.textAlignment = NSTextAlignmentLeft;
    provinceLabel.text = [NSString stringWithFormat:@"省份：%@",model.province];
    [_backView addSubview:provinceLabel];
    
    [provinceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backView.mas_left).with.offset(ScreenWidth * 0.02);
        make.right.equalTo(_backView.mas_right).with.offset(- ScreenWidth * 0.02);
        make.top.equalTo(_backView.mas_top).with.offset(ScreenWidth * 0.02);
        make.height.mas_equalTo(ScreenWidth * 0.05);
    }];
    
    UILabel *cityLabel = [[UILabel alloc] init];
    cityLabel.font = [UIFont systemFontOfSize:13];
    cityLabel.textAlignment = NSTextAlignmentLeft;
    cityLabel.text = [NSString stringWithFormat:@"城市：%@",model.city];
    [_backView addSubview:cityLabel];
    
    [cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backView.mas_left).with.offset(ScreenWidth * 0.02);
        make.right.equalTo(_backView.mas_right).with.offset(- ScreenWidth * 0.02);
        make.top.equalTo(provinceLabel.mas_bottom).with.offset(ScreenWidth * 0.02);
        make.height.mas_equalTo(ScreenWidth * 0.05);
    }];
    
    UILabel *zreacodeLabel = [[UILabel alloc] init];
    zreacodeLabel.font = [UIFont systemFontOfSize:13];
    zreacodeLabel.textAlignment = NSTextAlignmentLeft;
    zreacodeLabel.text = [NSString stringWithFormat:@"区号：%@",model.zreacode];
    [_backView addSubview:zreacodeLabel];
    
    [zreacodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backView.mas_left).with.offset(ScreenWidth * 0.02);
        make.right.equalTo(_backView.mas_right).with.offset(- ScreenWidth * 0.02);
        make.top.equalTo(cityLabel.mas_bottom).with.offset(ScreenWidth * 0.02);
        make.height.mas_equalTo(ScreenWidth * 0.05);
    }];
    
    UILabel *zipLabel = [[UILabel alloc] init];
    zipLabel.font = [UIFont systemFontOfSize:13];
    zipLabel.textAlignment = NSTextAlignmentLeft;
    zipLabel.text = [NSString stringWithFormat:@"邮编：%@",model.zip];
    [_backView addSubview:zipLabel];
    
    [zipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backView.mas_left).with.offset(ScreenWidth * 0.02);
        make.right.equalTo(_backView.mas_right).with.offset(- ScreenWidth * 0.02);
        make.top.equalTo(zreacodeLabel.mas_bottom).with.offset(ScreenWidth * 0.02);
        make.height.mas_equalTo(ScreenWidth * 0.05);
    }];
    
    UILabel *companyLabel = [[UILabel alloc] init];
    companyLabel.font = [UIFont systemFontOfSize:13];
    companyLabel.textAlignment = NSTextAlignmentLeft;
    companyLabel.text = [NSString stringWithFormat:@"运营商：%@",model.company];
    [_backView addSubview:companyLabel];
    
    [companyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backView.mas_left).with.offset(ScreenWidth * 0.02);
        make.right.equalTo(_backView.mas_right).with.offset(- ScreenWidth * 0.02);
        make.top.equalTo(zipLabel.mas_bottom).with.offset(ScreenWidth * 0.02);
        make.height.mas_equalTo(ScreenWidth * 0.05);
    }];

    UILabel *cardLabel = [[UILabel alloc] init];
    cardLabel.font = [UIFont systemFontOfSize:13];
    cardLabel.textAlignment = NSTextAlignmentLeft;
    cardLabel.text = [NSString stringWithFormat:@"卡类型：%@",model.card];
    [_backView addSubview:cardLabel];
    
    [cardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backView.mas_left).with.offset(ScreenWidth * 0.02);
        make.right.equalTo(_backView.mas_right).with.offset(- ScreenWidth * 0.02);
        make.top.equalTo(companyLabel.mas_bottom).with.offset(ScreenWidth * 0.02);
        make.height.mas_equalTo(ScreenWidth * 0.05);
    }];
}

- (void)textFieldDidChange:(UITextField *)TextField {
    if (TextField.text.length == 0) {
        if (_backView) {
            [_backView removeFromSuperview];
        }
    }
    
}
@end
